package binarycrew.livewallpapers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import binarycrew.livewallpapers.wallpapers.ALiveWallpaper;
import binarycrew.livewallpapers.wallpapers.Wallpapers;

/**
 * User: Richard MihaloviÄ�
 */
public class PreviewActivity extends Activity {

	private final Handler handler = new Handler();
	private Long startTime = 0L;

	private ALiveWallpaper wallpaper;

	private Preview preview = null;

	private Button buttonSet;

	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.preview);

		createDefaultImage();

		Bundle bundle = this.getIntent().getExtras();
		wallpaper = Wallpapers.findWallpaperByCode(
				bundle.getString("wallpaper")
				);

		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();

		wallpaper.setScreenSize(width, height);

		preview = (Preview) findViewById(R.id.view);
		preview.setWallpaper(wallpaper);

		buttonSet = (Button) findViewById(R.id.setButton);
		buttonSet.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				SharedPreferences settings = getSharedPreferences("LiveWallpapers", 0);
				SharedPreferences.Editor editor = settings.edit();

				editor.putString("wallpaper", wallpaper.getCode());
				editor.commit();

				PreviewActivity.this.finish();
			}
		});

		Toast.makeText(this, wallpaper.getName(), 1000).show();
	}

	private void createDefaultImage() {
		File f = new File(Preferences.getFilesDir(), Preferences.DEFAULT_IMAGE);
		if (f.exists())
			return;

		Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.background);

		FileOutputStream fos;
		try {
			fos = new FileOutputStream(f);
			icon.compress(Bitmap.CompressFormat.JPEG, 90, fos);
			fos.close();
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		handler.removeCallbacks(updateTimerTask);
	}

	@Override
	protected void onResume() {
		super.onResume();

		setupTimer();
	}

	private void setupTimer() {
		if (startTime == 0L) {
			startTime = System.currentTimeMillis();
			handler.removeCallbacks(updateTimerTask);
			handler.postDelayed(updateTimerTask, (int) (1000 / wallpaper.getFPS()));
		}
	}

	private final Runnable updateTimerTask = new Runnable() {

		@Override
		public void run() {
			if (preview != null)
				preview.invalidate();

			handler.postDelayed(updateTimerTask, (int) (1000 / wallpaper.getFPS()));
		}
	};
}
